#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <queue>
using namespace std;
struct nod{
	int left, right;//无子树的值为-1
}node[10];
bool notroot[10] = {false};
queue<int> que;
void level(int root)
{
	int num =  0;
	que.push(root);
	while(!que.empty())
	{
		if(0 == num)printf("%d", que.front());
		else printf(" %d", que.front());
		if(-1 != node[que.front()].left)que.push(node[que.front()].left);
		if(-1 != node[que.front()].right)que.push(node[que.front()].right);
		que.pop();
		++num;
	}
}
int num = 0;
void post(int root)
{
	if(-1 == root)return;
	else
	{
		post(node[root].left);
		if(0 == num)printf("%d", root);
		else printf(" %d", root);
		++num;
		post(node[root].right);
	}
	return;	
}
main()
{
	int N; 	
	scanf("%d", &N);
	char c[2];
	getchar();//读取行尾的换行符
	for(int i = 0; i < N; ++i)
	{//读取的时候反过来读，
		scanf("%s", c);	
		if(!strcmp(c, "-"))
		{
			node[i].right = -1;				
		}else
		{
			notroot[atoi(c)] = true;
			node[i].right = atoi(c);
		}
		scanf("%s", c);	
		if(!strcmp(c, "-"))
		{
			node[i].left = -1;				
		}else
		{
			notroot[atoi(c)] = true;
			node[i].left = atoi(c);
		}
	}
	int root;
	for(root = 0; notroot[root]; ++root);//找根节点
	level(root);
	putchar('\n');
	post(root);
	putchar('\n');
	return 0;	
}